package com.itjeffrey.autocode.controller;

import com.itjeffrey.autocode.common.Result;
import com.itjeffrey.autocode.service.CodeGenerateService;
import com.itjeffrey.autocode.bo.req.DbLinkBO;
import com.itjeffrey.autocode.bo.resp.DataSourceVO;
import com.itjeffrey.autocode.bo.resp.DbLinkVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @From: Jeffrey
 * @Date: 2020/11/11
 */
@RestController
@RequestMapping("/template/code")
public class CodeGenerateController implements BaseUploadController<MultipartFile, DataSourceVO> {

    @Resource
    private CodeGenerateService codeGenerateService;

    /**
     * 测试数据库连接
     *
     * @param dbLinkBo
     * @return
     */
    @PostMapping("/dbLink")
    public ResponseEntity<DbLinkVo> dbLink(@RequestBody DbLinkBO dbLinkBo) {
        return ResponseEntity.ok(codeGenerateService.dbLink(dbLinkBo));
    }

    /**
     * 代码预览
     *
     * @param dbLinkBo
     * @return
     */
    @PostMapping("/view")
    public ResponseEntity<List<Map<String, String>>> codeView(@RequestBody DbLinkBO dbLinkBo) {
        return ResponseEntity.ok(codeGenerateService.codeView(dbLinkBo));
    }

    /**
     * 获取当前系统connection连接数
     *
     * @return
     */
    @PostMapping("/getcurConnCount")
    public ResponseEntity<Result> getcurConnCount() {
        return ResponseEntity.ok(codeGenerateService.getcurConnCount());
    }

    /**
     * 数据库连接文件上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseEntity<Result<DataSourceVO>> upload(MultipartFile multipartFile) {
        return ResponseEntity.ok(codeGenerateService.upload(multipartFile));
    }

    /**
     * 数据库连接文件模板下载
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/dsFileModuleDownload")
    public ResponseEntity<Void> dsFileModuleDownload(HttpServletRequest request, HttpServletResponse response) {
        codeGenerateService.dsFileModuleDownload(request, response);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 一键生成文档
     *
     * @param dbLinkBo
     * @return
     */
    @PostMapping("/oneKeyGenerate")
    public ResponseEntity<Void> oneKeyGenerate(@RequestBody DbLinkBO dbLinkBo) {
        codeGenerateService.oneKeyGenerate(dbLinkBo);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
}
